package lwh.adapter;

import java.util.List;

import lwh.activity.MyGlobal;
import lwh.activity.R;
import lwh.domain.ContractChild;
import lwh.domain.ContractGroup;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class TemplateCheckCopyExpandableListAdapter extends
		BaseExpandableListAdapter {

private List<ContractGroup> list;
	
    private LayoutInflater inflater;
    
    private MyGlobal global;
    
    private Activity activity;
    
    public TemplateCheckCopyExpandableListAdapter(Context context, List<ContractGroup> list, MyGlobal global) {
    	this.inflater = LayoutInflater.from(context);
    	this.list = list;
    	this.global = global;
    	this.activity = (Activity) context;
	}
	public Object getChild(int groupPosition, int childPosition) {
		return list.get(groupPosition).getChildren().get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
            convertView = inflater.inflate(R.layout.templatecheck_children, null);
        }
		ContractChild child = list.get(groupPosition).getChildren().get(childPosition);
		TextView Tem_exm_item_name = (TextView) convertView.findViewById(R.id.Tem_exm_item_name);
		Tem_exm_item_name.setText(child.getExm_item_name());
		TextView Tem_exm_item_crt_name = (TextView) convertView.findViewById(R.id.Tem_exm_item_crt_name);
		Tem_exm_item_crt_name.setText(child.getExm_item_crt_name());
		TextView Tem_prj_exm_item_result = (TextView) convertView.findViewById(R.id.Tem_prj_exm_item_result);
		Tem_prj_exm_item_result.setText(child.getPrj_exm_item_result());
		return convertView;
	}

	public int getChildrenCount(int groupPosition) {
		return list.get(groupPosition).getChildren().size();
	}

	public Object getGroup(int groupPosition) {
		return list.get(groupPosition);
	}

	public int getGroupCount() {
		return list.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
            convertView = inflater.inflate(R.layout.templatecheck_group, null);
        }
		final ContractGroup group = list.get(groupPosition);
		TextView text = (TextView) convertView.findViewById(R.id.parent_group);
		text.setText(group.getTitle());
		convertView.findViewById(R.id.view_btn).setOnClickListener(new OnClickListener() {
			   
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.view_btn: 
					String exm_item_type = (String)group.getExm_item_type();
					String exm_item_id = (String)group.getId();
					String prj_exm_item_id = (String)group.getPrj_exm_item_id();
					if(exm_item_type.equals("1")){
						Intent intent=new Intent();
						intent.setClassName("lwh.activity","lwh.activity.WordDescriptionActivity");
						Bundle bundle=new Bundle();
						String exm_ckp_id = (String)group.getExm_ckp_id();
						bundle.putString("exm_ckp_id", exm_ckp_id);
						bundle.putString("exm_item_id", exm_item_id);
						bundle.putString("prj_exm_item_id", prj_exm_item_id);
						bundle.putString("prj_exm_id", (String)group.getPrj_exm_id());
				    	intent.putExtras(bundle);
				    	activity.startActivityForResult(intent, 201);
					}
					else if(exm_item_type.equals("2")){
						Intent intent=new Intent();
						intent.setClassName("lwh.activity","lwh.activity.DataRangeActivity");
						Bundle bundle=new Bundle();
						bundle.putString("exm_item_id", exm_item_id);
						bundle.putString("prj_exm_item_id", prj_exm_item_id);
						bundle.putString("prj_exm_id", (String)group.getPrj_exm_id());
				    	intent.putExtras(bundle);
				    	activity.startActivityForResult(intent, 202);
					}
					else if(exm_item_type.equals("3")){
						Intent intent=new Intent();
						intent.setClassName("lwh.activity","lwh.activity.ConditionDataRangeActivity");
						Bundle bundle=new Bundle();
						bundle.putString("exm_item_id", exm_item_id);
						bundle.putString("prj_exm_item_id", prj_exm_item_id);
						bundle.putString("prj_exm_id", (String)group.getPrj_exm_id());
				    	intent.putExtras(bundle);
				    	activity.startActivityForResult(intent, 203);
					}
					else if(exm_item_type.equals("4")){
						Intent intent=new Intent();
						intent.setClassName("lwh.activity","lwh.activity.DesignDataRangeActivity");
						Bundle bundle=new Bundle();
						bundle.putString("exm_item_id", exm_item_id);
						bundle.putString("prj_exm_item_id", prj_exm_item_id);
						bundle.putString("prj_exm_id", (String)group.getPrj_exm_id());
				    	intent.putExtras(bundle);
				    	activity.startActivityForResult(intent, 204);
					}
			    				    	
					break;
				}
			}
			
		});
		return convertView;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

}
